package text;

import java.io.*;
import java.util.ArrayList;

public class AreaService {

    public AreaService() {
    }

    /**
     * @param mobile 手机号
     * @return 返回的地区，如果未查到，返回null
     */
    public String getArea(String mobile) {
        //14587810900, 1458781	3	四川.成都
        String substring = mobile.substring(0, 7);
        try {
            return read(substring);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String read(String mobile) throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("src/main/java/com/hh/text/area.txt"));
        ArrayList<String> arrayList = new ArrayList<>();
        ArrayList<String> arrayList1 = new ArrayList<>();
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            String[] split = line.split("\t");
            arrayList.add(split[0]);
            arrayList1.add(split[2]);
        }
        int i = arrayList.indexOf(mobile);
        if (i == -1) {
            return null;
        }
        return arrayList1.get(i);
    }

//    public static void main(String[] args) throws IOException {
//        BufferedReader bufferedReader = new BufferedReader(new FileReader("src/main/java/com/hh/text/area.txt"));
//        ArrayList<String> arrayList = new ArrayList<>();
//        ArrayList<String> arrayList1 = new ArrayList<>();
//        String line = "";
//        while ((line = bufferedReader.readLine()) != null) {
//            String[] split = line.split("\t");
//            arrayList.add(split[0]);
//            arrayList1.add(split[2]);
//        }
//        System.out.println(arrayList1.get(arrayList.indexOf("1999942")));
//    }
}
